/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess.Utilities
*        文件名:             MySqlHelper.cs
*        创建系统时间:       2012/11/2 19:07:01
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess.Utilities
{
    using System;
    using System.Data;
    using System.Data.Common;
    using MySql.Data.MySqlClient;

    public class MySqlHelper : BaseDbHelper, IDbHelper
    {
        public MySqlCommand command;
        public MySqlConnection connection;
        public MySqlDataAdapter dataAdapter;
        public MySqlTransaction transaction;

        public MySqlHelper()
        {
            this.connection = new MySqlConnection();
            this.command = new MySqlCommand();
            this.dataAdapter = new MySqlDataAdapter();
            base.FileName = "MySqlHelper.txt";
        }

        public MySqlHelper(string connectionString)
            : this()
        {
            this.connection.ConnectionString = connectionString;
        }

        public override void AttachAdapterParameters(IDataParameter[] commandParameters)
        {
            this.AttachParameters(this.dataAdapter.SelectCommand, commandParameters);
        }

        public override void AttachParameters(IDbCommand TargetDbCommand, IDataParameter[] commandParameters)
        {
            ((MySqlCommand)TargetDbCommand).Parameters.AddRange(commandParameters);
        }

        public string GetDBDateTime()
        {
            string sqlQuery = " SELECT " + this.GetDBNow();
            base.Open();
            string str2 = base.ExecuteScalar(CommandType.Text, sqlQuery, null).ToString();
            base.Close();
            return str2;
        }

        public string GetDBNow()
        {
            return " now() ";
        }

        public string GetParameter(string parameter)
        {
            return (" ?" + parameter);
        }

        public DbProviderFactory Instance()
        {
            return MySqlClientFactory.Instance;
        }

        public DbParameter MakeInParam(string targetFiled, object targetValue)
        {
            MySqlParameter parameter = null;
            if (targetValue != null)
            {
                if (targetValue is string)
                {
                    parameter = new MySqlParameter(targetFiled, MySqlDbType.VarChar);
                }
                else if (targetValue is bool)
                {
                    parameter = new MySqlParameter(targetFiled, MySqlDbType.Bit);
                }
                else if (targetValue is DateTime)
                {
                    parameter = new MySqlParameter(targetFiled, MySqlDbType.Date);
                }
                else if (targetValue is int)
                {
                    parameter = new MySqlParameter(targetFiled, MySqlDbType.Int32);
                }
                else if (targetValue is long)
                {
                    parameter = new MySqlParameter(targetFiled, MySqlDbType.Int64);
                }
                else if (targetValue is double)
                {
                    parameter = new MySqlParameter(targetFiled, MySqlDbType.Double);
                }
                else if (targetValue is byte[])
                {
                    parameter = new MySqlParameter(targetFiled, MySqlDbType.Binary);
                }
                else
                {
                    parameter = new MySqlParameter(targetFiled, MySqlDbType.VarChar);
                }
                parameter.Value = targetValue;
            }
            return parameter;
        }

        public DbParameter MakeInParam(string paramName, DbType DbType, int Size, object Value)
        {
            return this.MakeParam(paramName, DbType, Size, ParameterDirection.Input, Value);
        }

        public DbParameter MakeOutParam(string paramName, DbType DbType, int Size)
        {
            return this.MakeParam(paramName, DbType, Size, ParameterDirection.Output, null);
        }

        public DbParameter MakeParam(string paramName, DbType DbType, int Size, ParameterDirection Direction, object Value)
        {
            MySqlParameter parameter;
            if (Size > 0)
            {
                parameter = new MySqlParameter(paramName, (MySqlDbType)DbType, Size);
            }
            else
            {
                parameter = new MySqlParameter(paramName, (MySqlDbType)DbType);
            }
            parameter.Direction = Direction;
            if ((Direction != ParameterDirection.Output) || (Value != null))
            {
                parameter.Value = Value;
            }
            return parameter;
        }

        public DbParameter[] MakeParameters(string[] targetFileds, object[] targetValues)
        {
            DbParameter[] parameterArray = new DbParameter[0];
            if ((targetFileds != null) && (targetValues != null))
            {
                parameterArray = new DbParameter[targetFileds.Length];
                for (int i = 0; i < targetFileds.Length; i++)
                {
                    parameterArray[i] = this.MakeInParam(targetFileds[i], targetValues[i]);
                }
            }
            return parameterArray;
        }

        public DbParameter[] MakeParameters(string targetFiled, object targetValue)
        {
            DbParameter[] parameterArray = null;
            if ((targetFiled != null) && (targetValue != null))
            {
                parameterArray = new DbParameter[] { this.MakeInParam(targetFiled, targetValue) };
            }
            return parameterArray;
        }

        public string PlusSign(params string[] values)
        {
            string str = string.Empty;
            str = " CONCAT(";
            for (int i = 0; i < values.Length; i++)
            {
                str = str + values[i] + " ,";
            }
            return (str.Substring(0, str.Length - 2) + ")");
        }

        public string SqlSafe(string value)
        {
            value = value.Replace("'", "''");
            return value;
        }
    }
}